package com.mondo.financialwallet.presentation.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.common.ui.AbsNetworkStateActivity;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.data.entity.MyTokenListEntity;
import com.mondo.financialwallet.presentation.model.Token;
import com.mondo.financialwallet.presentation.ui.adapter.TokenListAdapter;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 云端资产
 */
public class CloudAssetsActivity extends AbsNetworkStateActivity implements BaseQuickAdapter.OnItemClickListener {

    private static final String EXTRA_CLOUD_ASSET = "CLOUD_ASSET";

    public static void launch(Context context, String CloudAsset) {
        context.startActivity(
                new Intent(context, CloudAssetsActivity.class)
                        .putExtra(EXTRA_CLOUD_ASSET, CloudAsset)
        );
    }

    @Bind(R.id.cloud_asset_text) TextView mCloudAssetText;
    @Bind(R.id.cloud_asset_list) RecyclerView mCloudAssetList;

    private TokenListAdapter mAdapter;

    @Override
    protected void setStatusBar() {
        ActivityTitleBarHelper.setTransparentStatus(this);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_cloud_asset;
    }

    @Override
    protected void initActions() {
        ActivityTitleBarHelper.init(this, R.string.title_cloud_assets);
        mCloudAssetText.setText(getIntent().getStringExtra(EXTRA_CLOUD_ASSET));

        // 请求我的货币列表
        requestMyTokenList();
    }

    private void requestMyTokenList() {
        loadingNetwork();

        DataRepository.get().myTokenList(result -> {
            successNetwork();

            if (result.isSuccess()) {
                mAdapter = new TokenListAdapter(
                        R.layout.item_cloud_asset,
                        transformToken(result.getData())
                );
                mAdapter.setOnItemClickListener(this);
                mCloudAssetList.setAdapter(mAdapter);
            }
        });
    }

    private List<Token> transformToken(List<MyTokenListEntity.DataBean> data) {
        List<Token> tokens = new ArrayList<>();
        for (MyTokenListEntity.DataBean datum : data) {
            tokens.add(new Token(
                    datum.getLogo(),
                    datum.getToken_id(),
                    datum.getTitle_en(),
                    String.valueOf(datum.getUsd()),
                    String.valueOf(datum.getBalance())));
        }

        return tokens;
    }

    @Override
    public void onAnewRequest() {
        requestMyTokenList();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (mAdapter != null){
            Token token = mAdapter.getItem(position);
            TokenTransferActivity.launch(this, token);
        }
    }
}
